<%@page import="com.icss.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%User u = (User)session.getAttribute("user"); %>
<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
  <meta name="author" content="mironcoder" />
  <meta name="email" content="mironcoder@gmail.com" />
  <meta name="profile" content="https://themeforest.net/user/mironcoder" />
  <meta name="template" content="Organe" />
  <meta name="title" content="Organe - Organic Food HTML Template" />
  <meta name="keywords" content="organe, organic, food, shop, ecommerce, store, html, agriculture, vegetable, organic food, organic product, organic farm, organic life, organic shop, food store" />
  <title>Organe - Home Index</title>
  <link rel="icon" href="images/favicon.png" />
  <link rel="stylesheet" href="static/css/flaticon.css" />
  <link rel="stylesheet" href="static/css/icofont.min.css" />
  <link rel="stylesheet" href="static/css/nice-select.min.css" />
  <link rel="stylesheet" href="static/css/jquery-ui.min.css" />
  <link rel="stylesheet" href="static/css/venobox.min.css" />
  <link rel="stylesheet" href="static/css/slick.min.css" />
  <link rel="stylesheet" href="static/css/bootstrap.min.css" />
  <link rel="stylesheet" href="static/css/main.css" />
  <link rel="stylesheet" href="static/css/index.css" />
 </head>
 <body>
  <section class="header-top">
   <div class="container">
    <div class="row">
     <div class="col-md-12 col-lg-5">
      <div class="header-top-welcome">
       <p>Welcome to Ecomart in Your Dream Online Store!</p>
      </div>
     </div>
     <div class="col-md-5 col-lg-3">
      <div class="header-top-select">
       <div class="header-select">
        <i class="icofont-world"></i>
        <select class="select"><option value="english" selected="">中文</option></select>
       </div>
       <div class="header-select">
        <i class="icofont-money"></i>
        <select class="select"><option value="english" selected="">￥</option></select>
       </div>
      </div>
     </div>
     <div class="col-md-7 col-lg-4">
      <ul class="header-top-list">
       <li><a href="#">offers</a></li>
       <li><a href="#">need help</a></li>
       <li><a href="UserServlet?flag=loginOut">退出登录</a></li>
      </ul>
     </div>
    </div>
   </div>
  </section>
  <section class="header-part">
   <div class="container-fluid">
    <div class="header-content-group">
     <div class="header-widget-group left" style="width: 400px">
       <%if(u==null){ %>
     	<a class="header-widget" href="login.jsp" title="登录"><img src="static/picture/user.png" alt="user" /><span>join here</span></a>
     	<%}else{ %>
     	<%if(u.getType() == 0){ %>
			<p>欢迎管理员:<%=u.getName() %>登录</p>
		<%}else{ %>
			<p>欢迎用户:<%=u.getName() %>登录</p>
		<%} %>
     <%} %>
     </div>
     <div class="header-form">
   		
     	 	<input type="text" id="stuName" placeholder="搜索商品" />
      		<button onclick="findByPageName(1)"><i class="icofont-ui-search"></i></button>
    	 
     </div>
    </div>
   
   </div>
  </section>
 <section class="section recent-part">
   <div class="container">
    <div class="row">
     <div class="col-lg-12">
      <div class="section-heading">
       <h2>商品信息</h2>
      </div>
     </div>
    </div>
    <div class="row">
     	 <%if(u.getType() == 0){ %>
		<a href="add.jsp">添加商品信息</a>
	<%} %>	
	<table id="tab" style="text-align: center" cellpadding="10px">
		<tr>
			<th>商品编号</th>
			<th>商品名</th>
			<th>类别</th>
			<th>价格</th>
			<%if(u.getType() == 0){ %>
				<input type="hidden" id="t" value="<%=u.getType()%>"/>
				<th>操作</th>
			<%} %>			
		</tr>
	</table>
	<span id="total"></span>
	<span id="current"></span>
	
     </div>
     <span id="up"></span>
	<span id="down"></span>
	<span id="pc"></span>
    </div>
   
  </section>
 
	 <script src="static/js/jquery-1.12.4.min.js"></script>
  <script src="static/js/popper.min.js"></script>
  <script src="static/js/bootstrap.min.js"></script>
  <script src="static/js/nice-select.min.js"></script>
  <script src="static/js/venobox.min.js"></script>
  <script src="static/js/jquery-ui.js"></script>
  <script src="static/js/slick.min.js"></script>
  <script src="static/js/nice-select.js"></script>
  <script src="static/js/price-range.js"></script>
  <script src="static/js/accordion.js"></script>
  <script src="static/js/venobox.js"></script>
  <script src="static/js/slick.js"></script>
  <script src="static/js/main.js"></script>
	<script type="text/javascript" src="jq/jquery-1.8.3.js"></script>
	<script type="text/javascript">
		$(function(){
			findByPageName(1);
		});
		function findByPageName(page){
			$.ajax({
				url:"StudentServlet",
				type:"get",
				data:{name:$("#stuName").val(),current:page,flag:"findByPageName"},
				dataType:"json",
				success:function(data){
					$("#total").text("共有"+data.totalPage+"页,");
					$("#current").text("当前第"+data.currentPage+"页");
					/* 上一页 */
					$("#up a").remove();
					if(data.currentPage != 1){
						var a = "<a href='#' onclick='findByPageName("+(data.currentPage-1)+")'>上一页</a>";
						$("#up").html(a);
					}
					/* 下一页 */
					$("#down a").remove();
					if(data.currentPage != data.totalPage){
						var a = "<a href='#' onclick='findByPageName("+(data.currentPage+1)+")'>下一页</a>";
						$("#down").html(a);
					}
					/* 页码 */
					$("#pc a").remove();
					for(var i = 1;i <= data.totalPage;i++){
						var a = "<a href='#' onclick='findByPageName("+i+")'>&nbsp;&nbsp;"+i+"&nbsp;&nbsp;</a>";
						$("#pc").append(a);
					}
					//处理数据
					$("#tab tr:gt(0)").remove();
					$.each(data.list,function(i,o){
						if($("#t").val() == 0){
							var tr = "<tr>"+
							"<td>"+o.id+"</td>"+
							"<td>"+o.name+"</td>"+
							"<td>"+o.sex+"</td>"+
							"<td>"+o.tid+"</td>"+
							"<td>"+
							"<a href='#' onclick='del("+o.id+")'>删除</a>&nbsp;&nbsp;&nbsp;&nbsp;"+
							"<a href='StudentServlet?sid="+o.id+"&flag=findById'>修改</a>"+
							"</td>"+
							"</tr>";
							$("#tab").append(tr);
						}else{
							var tr = "<tr>"+
							"<td>"+o.id+"</td>"+
							"<td>"+o.name+"</td>"+
							"<td>"+o.sex+"</td>"+
							"<td>"+o.tid+"</td>"+
							"</tr>";
							$("#tab").append(tr);
						}
						
					});
				},
				error:function(data){
					alert("失败！");
				}
			});
		}
		function del(id){
			$.ajax({
				url:"StudentServlet",
				type:"get",
				data:{sid:id,flag:"del"},
				dataType:"text",
				success:function(data){
					if(data == "成功"){
						alert("删除成功！");
						findByPageName(1);
					}else{
						alert("删除失败");
					}					
				},
				error:function(data){
					alert("失败！");
				}
			});
		}
	</script>
 
 </body>
</html>